package com.tnt.notice;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Toast;

public class NoticeAdapter extends BaseAdapter {

    // RSS Info.
	public ArrayList<NoticeInfo> getData(String url) {

		NoticeInfo mItem = null;
		ArrayList<NoticeInfo> mItemList = new ArrayList<NoticeInfo>();

		String sTag;
		int    i = 0;

		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			XmlPullParser xpp = factory.newPullParser();

			URL u = new URL(url);
			InputStream in = u.openStream();
			xpp.setInput(in, "utf-8");

			int eventType = xpp.getEventType();

			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_DOCUMENT) {
					// System.out.println("Start document");
				} else if (eventType == XmlPullParser.END_DOCUMENT) {
					// System.out.println("End document");
				} else if (eventType == XmlPullParser.START_TAG) {

					//Log.e("START_TAG", xpp.getName());
					sTag = xpp.getName();

					if (sTag.equals("title")) {
						mItem = new NoticeInfo();
						mItem.setTitle(getHtml2Text(xpp.nextText()));								 
						//Log.d("RSS Title : ", mItem.title);
					}
					if (sTag.equals("link")) {
						mItem.setLink(xpp.nextText());
					}
					if (sTag.equals("description")) {
						mItem.setDescription(getHtml2Text(xpp.nextText()));
					}
					if (sTag.equals("date")) {
						mItem.setPubdate(xpp.nextText());
					}

				} else if (eventType == XmlPullParser.END_TAG) {
					sTag = xpp.getName();
					if (sTag.equals("item")) {
						mItem.setIndex(i);
						mItemList.add(mItem);
						mItem = null;
						i++;
					}
				} else if (eventType == XmlPullParser.TEXT) {
					//Log.e("GG", "getRssmItem : " + xpp.getText());
				}
				eventType = xpp.next();
			}

		} catch (Exception e) {

		}
		return mItemList;
	}

	private static String getHtml2Text(String str) {
		CharSequence styledString = Html.fromHtml(str);
		return styledString.toString();
	}
	
	@Override
	public int getCount() {
		return 0;
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		return null;
	}
    

}
